﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using System.Reflection;
using System.Configuration; 

namespace Runda.EShop.Common.Utility
{
    public abstract class BllBase : CacheBase
    {
        #region Private Variables
        DalBase dal = null; 
        #endregion

        #region Construced Function
        public BllBase()
        {
            dal = new DalBase();
        }

        public BllBase(string dbName)
        {
            dal = new DalBase(dbName);
        }
        #endregion

        #region Protected Method

        /// <summary>
        /// 根据DataRow数据构建Info信息实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="entityInfo">需要构建的实体</param>
        /// <param name="entityInfoData">实体信息</param>
        protected T BuildEntityFromDataRow<T>(DataRow entityInfoData)
        {
            object entityInfo = null;
            Type entityType = typeof(T);

            entityInfo = Activator.CreateInstance(entityType);

            PropertyInfo[] fields = entityType.GetProperties();

            foreach (PropertyInfo field in fields)
            {
                if (entityInfoData.Table.Columns.IndexOf(field.Name) < 0 ||
                    entityInfoData[field.Name] == null ||
                    entityInfoData[field.Name] == DBNull.Value)
                {
                    continue;
                }

                field.SetValue(entityInfo, entityInfoData[field.Name], null);
            }

            return (T)entityInfo;
        }

        protected List<T> BuildEntityListFromDataTable<T>(DataTable dt)
        {
            List<T> list = new List<T>();

            foreach (DataRow row in dt.Rows)
            {
                list.Add(BuildEntityFromDataRow<T>(row));
            }
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterString"></param>
        /// <returns></returns>
        protected DataTable QuerySubDataTable(DataTable allDatas, string filterString)
        {
            DataRow[] dictionaryDatas = this.QuerySubDataRowCollection(allDatas, filterString);
            DataTable queryResult = null;

            if (dictionaryDatas != null)
            {
                queryResult = allDatas.Clone();
                foreach (DataRow dictionaryData in dictionaryDatas)
                {
                    queryResult.ImportRow(dictionaryData);
                }

                queryResult.AcceptChanges();
            }

            return queryResult;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="allDatas"></param>
        /// <param name="filterString"></param>
        /// <returns></returns>
        protected DataRow[] QuerySubDataRowCollection(DataTable allDatas, string filterString)
        {
            DataRow[] dictionaryDatas = null;

            if (allDatas != null)
            {
                dictionaryDatas = allDatas.Select(filterString);
            }

            return dictionaryDatas;

        }

        #endregion

        #region public method

        public virtual void Insert(object objEntity)
        {
            dal.Insert(objEntity);
            this.Flush();
        }

        public virtual void Update(object objEntity)
        {
            dal.Update(objEntity);
            this.Flush();
        }

        public virtual void InsertOrUpdate(object objEntity)
        {
            dal.InsertOrUpdate(objEntity);
            this.Flush();
        }

        public virtual void Delete(object objEntity)
        {
            dal.Delete(objEntity);
            this.Flush();
        }

        public virtual void Delete<T>(List<SqlDbParameter> parameters)
        {
            dal.Delete<T>(parameters);
            this.Flush();
        }

        public virtual void Delete<T>(string WhereClause)
        {
            dal.Delete<T>(WhereClause);
            this.Flush();
        }

        public virtual void LogicDelete(object objEntity)
        {
            dal.LogicDelete(objEntity);
            this.Flush();
        }

        public virtual void LogicDelete<T>(List<SqlDbParameter> parameters)
        {
            dal.Delete<T>(parameters);
            this.Flush();
        }

        public virtual void LogicDelete<T>(string WhereClause)
        {
            dal.LogicDelete<T>(WhereClause);
            this.Flush();
        }

        public virtual T Get<T>(object primaryID)
        {
            return dal.Get<T>(primaryID);
        }

        public virtual T Get<T>(string whereClause)
        {
            return dal.Get<T>(whereClause);
        }

        public virtual T Get<T>(List<SqlDbParameter> parameters)
        {
            return dal.Get<T>(parameters);
        }

        public virtual List<T> GetAll<T>(List<SqlDbParameter> parameters, string OrderBy)
        {
            return dal.GetAll<T>(parameters, OrderBy);
        }

        public virtual List<T> GetAll<T>(List<SqlDbParameter> parameters)
        {
            return dal.GetAll<T>(parameters);
        }

        public List<T> GetAll<T>(string whereClause)
        {
            return dal.GetAll<T>(whereClause);
        }

        public List<T> GetAll<T>(string whereClause, string OrderBy)
        {
            return dal.GetAll<T>(whereClause, OrderBy);
        }

        public virtual List<T> GetAll<T>(List<SqlDbParameter> parameters, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            return dal.GetAll<T>(parameters, OrderBy, PageSize, PageIndex, out RecordCount);
        }

        public virtual List<T> GetAll<T>(string whereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            return dal.GetAll<T>(whereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }

        public virtual int Count<T>(string WhereClause)
        {
            return dal.Count<T>(WhereClause);
        }

        protected void SafeDbExecute(Action<IDbTransaction> action)
        {
            dal.SafeDbExecute(action);
        }
        #endregion
    }
}
